<!DOCTYPE html>

<html>
<head>
	<title>AIL-Framework</title>
	<link rel="icon" href="{{ url_for('static', filename='image/ail-icon.png')}}">
	<!-- Core CSS -->
	<link href="{{ url_for('static', filename='css/bootstrap4.min.css') }}" rel="stylesheet">
	<link href="{{ url_for('static', filename='css/font-awesome.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/daterangepicker.min.css') }}" rel="stylesheet">

	<!-- JS -->
	<script src="{{ url_for('static', filename='js/jquery.js')}}"></script>
	<script src="{{ url_for('static', filename='js/bootstrap4.min.js')}}"></script>
    <script src="{{ url_for('static', filename='js/moment.min.js') }}"></script>
 	<script src="{{ url_for('static', filename='js/jquery.daterangepicker.min.js') }}"></script>


</head>

<body>

	{% include 'nav_bar.html' %}

	<div class="container-fluid">
		<div class="row">

			{% include 'crawler/menu_sidebar.html' %}

			<div class="col-12 col-lg-10" id="core_content">

				{% include 'crawler/crawler_disabled.html' %}

				<div class="row">
					<div class="col-xl-4">

						<div class="card mt-1 mb-1">
						  <div class="card-header text-white bg-dark">
								<h5><a class="text-info" href="{{ url_for('crawler_splash.crawlers_last_domains')}}?type=onion"><i class="fas fa-user-secret"></i> Onions Crawlers</a></h5>
								<div class="row">
									<div class="col-5">
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=onion&up=True&date_from={{date}}&date_to={{date}}" class="badge badge-success" id="stat_onion_domain_up">{{ crawlers_latest_stats['onion']['up'] }}</a> <small>UP</small>
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=onion&down=True&date_from={{date}}&date_to={{date}}" class="badge badge-danger ml-md-3" id="stat_onion_domain_down">{{ crawlers_latest_stats['onion']['down'] }}</a> <small>DOWN</small>
									</div>
									<div class="col-7">
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=onion&up=True&down=True&date_from={{date}}&date_to={{date}}" class="badge badge-success" id="stat_onion_total">{{ crawlers_latest_stats['onion']['crawled'] }}</a> <small>Crawled</small>
										<span class="badge badge-warning ml-md-3" id="stat_onion_queue">{{ crawlers_latest_stats['onion']['queue'] }}</span> <small>Queue</small>
									</div>
								</div>
							</div>
						</div>

					</div>
                    <div class="col-xl-4">
						<div class="card mt-1 mb-1">
							<div class="card-header text-white bg-dark">
								<h5><a class="text-info" href="{{ url_for('crawler_splash.crawlers_last_domains')}}?type=i2p"><i class="fas fa-user-secret"></i> I2P Crawlers</a></h5>
								<div class="row">
									<div class="col-5">
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=i2p&up=True&date_from={{date}}&date_to={{date}}" class="badge badge-success" id="stat_i2p_domain_up">{{ crawlers_latest_stats['i2p']['up'] }}</a> <small>UP</small>
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=i2p&down=True&date_from={{date}}&date_to={{date}}" class="badge badge-danger ml-md-3" id="stat_i2p_domain_down">{{ crawlers_latest_stats['i2p']['down'] }}</a> <small>DOWN</small>
									</div>
									<div class="col-7">
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=i2p&up=True&down=True&date_from={{date}}&date_to={{date}}" class="badge badge-success" id="stat_i2p_total">{{ crawlers_latest_stats['i2p']['crawled'] }}</a> <small>Crawled</small>
										<span class="badge badge-warning ml-md-3" id="stat_i2p_queue">{{ crawlers_latest_stats['i2p']['queue'] }}</span> <small>Queue</small>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="col-xl-4">
						<div class="card mt-1 mb-1">
							<div class="card-header text-white bg-dark">
								<h5><a class="text-info" href="{{ url_for('crawler_splash.crawlers_last_domains')}}?type=web"><i class="fab fa-html5"></i> Web Crawlers</a></h5>
								<div class="row">
									<div class="col-5">
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=web&up=True&date_from={{date}}&date_to={{date}}" class="badge badge-success" id="stat_web_domain_up">{{ crawlers_latest_stats['web']['up'] }}</a> <small>UP</small>
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=web&down=True&date_from={{date}}&date_to={{date}}" class="badge badge-danger ml-md-3" id="stat_web_domain_down">{{ crawlers_latest_stats['web']['down'] }}</a> <small>DOWN</small>
									</div>
									<div class="col-7">
										<a href="{{ url_for('crawler_splash.domains_search_date') }}?type=web&up=True&down=True&date_from={{date}}&date_to={{date}}" class="badge badge-success" id="stat_web_total">{{ crawlers_latest_stats['web']['crawled'] }}</a> <small>Crawled</small>
										<span class="badge badge-warning ml-md-3" id="stat_web_queue">{{ crawlers_latest_stats['web']['queue'] }}</span> <small>Queue</small>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>

				<table class="table">
					<tbody id="tbody_crawler_onion_info">
						{% for crawler in crawlers_status %}
							<tr>
								<td>
									<i class="fas fa-{%if crawler['status']%}check{%else%}times{%endif%}-circle" style="color:{%if crawler['status']%}Green{%else%}Red{%endif%};"></i> {{crawler['start_time']}}
								</td>
								<td>
									{%if crawler['type']=='onion'%}
										<i class="fas fa-user-secret"></i>
									{%else%}
										<i class="fab fa-html5">
									{%endif%}
								</td>
								<td>
									{{crawler['domain']}}
								</td>
								<td style="color:{%if crawler['status']%}Green{%else%}Red{%endif%};">
									{{crawler['status']}}
								</td>
							</tr>
						{% endfor %}
					</tbody>
				</table>

				{% include 'domains/block_domains_name_search.html' %}
                <div class="d-flex justify-content-center my-4">
                    {% include 'crawler/show_domains_by_daterange.html' %}
                </div>

				<hr>
				<div class="row mb-3">
					<div class="col-xl-6">
						<div class="text-center">
							<a class="btn btn-secondary" href="{{url_for('crawler_splash.domains_explorer_onion')}}" role="button">
								<i class="fas fa-user-secret"></i> Onion Domain Explorer
							</a>
						</div>
					</div>
					<div class="col-xl-6">
						<div class="text-center">
							<a class="btn btn-secondary" href="{{url_for('crawler_splash.domains_explorer_web')}}" role="button">
								<i class="fab fa-html5"></i> Web Domain Explorer
							</a>
						</div>
					</div>
				</div>



				{% with object_type='domain' %}
					{% include 'tags/block_obj_tags_search.html' %}
				{% endwith %}

				{% with object_type='domain' %}
					{% include 'domains/block_languages_search.html' %}
				{% endwith %}

			</div>
		</div>
	</div>


</body>

<script>
var to_refresh = false
$(document).ready(function(){
	$("#page-Crawler").addClass("active");
	$("#nav_dashboard").addClass("active");
    $('#date-range-from').dateRangePicker({
			separator : ' to ',
			getValue: function(){
					if ($('#date-range-from-input').val() && $('#date-range-to-input').val() )
							return $('#date-range-from-input').val() + ' to ' + $('#date-range-to-input').val();
					else
							return '';
					},
					setValue: function(s,s1,s2){
							$('#date-range-from-input').val(s1);
							$('#date-range-to-input').val(s2);
					}
	});
	$('#date-range-to').dateRangePicker({
			separator : ' to ',
			getValue: function(){
					if ($('#date-range-from-input').val() && $('#date-range-to-input').val() )
							return $('#date-range-from-input').val() + ' to ' + $('#date-range-to-input').val();
					else
							return '';
			},
			setValue: function(s,s1,s2){
					$('#date-range-from-input').val(s1);
					$('#date-range-to-input').val(s2);
			}
	});
	$( window ).on("focus", function() {
			to_refresh = true
			refresh_crawler_status();
	});
	$( window ).on("blur", function() {
			to_refresh = false
	});

	to_refresh = true
	refresh_crawler_status();

});

function toggle_sidebar(){
	if($('#nav_menu').is(':visible')){
		$('#nav_menu').hide();
		$('#side_menu').removeClass('border-right')
		$('#side_menu').removeClass('col-lg-2')
		$('#core_content').removeClass('col-lg-10')
	}else{
		$('#nav_menu').show();
		$('#side_menu').addClass('border-right')
		$('#side_menu').addClass('col-lg-2')
		$('#core_content').addClass('col-lg-10')
	}
}

function refresh_crawler_status(){

	$.getJSON("{{ url_for('crawler_splash.crawler_dashboard_json') }}",
		function(data) {

			$('#stat_onion_domain_up').text(data.stats['onion']['up']);
			$('#stat_onion_domain_down').text(data.stats['onion']['down']);
			$('#stat_onion_total').text(data.stats['onion']['crawled']);
			$('#stat_onion_queue').text(data.stats['onion']['queue']);

			$('#stat_web_domain_up').text(data.stats['web']['up']);
			$('#stat_web_domain_down').text(data.stats['web']['down']);
			$('#stat_web_total').text(data.stats['web']['crawled']);
			$('#stat_web_queue').text(data.stats['web']['queue']);

            $("#tbody_crawler_onion_info").empty();

			if(data.crawlers_status.length!=0){
				var tableRef = document.getElementById('tbody_crawler_onion_info');
				for (var i = 0; i < data.crawlers_status.length; i++) {
						var crawler = data.crawlers_status[i];
						var newRow = tableRef.insertRow(tableRef.rows.length);
						var text_color;
						var icon;
						if(crawler['status']){
							text_color = 'Green';
							icon = 'check';
						} else {
							text_color = 'Red';
							icon = 'times';
						}

						if(crawler['type'] === 'onion'){
							icon_t = 'fas fa-user-secret';
						} else {
							icon_t = 'fab fa-html5';
						}

				    var newCell  = newRow.insertCell(0);
						newCell.innerHTML = "<td><i class=\"fas fa-"+icon+"-circle\" style=\"color:"+text_color+";\"></i> "+crawler['start_time']+"</td>";

						var newCell  = newRow.insertCell(1);
						newCell.innerHTML = "<td><i class=\""+icon_t+"\"></i></td>";

						newCell  = newRow.insertCell(2);
						newCell.innerHTML = "<td>"+crawler['domain']+"</td>";

						newCell  = newRow.insertCell(3);
						newCell.innerHTML = "<td><div style=\"color:"+text_color+";\">"+crawler['status']+"</div></td>";

						//$("#panel_crawler").show();
				}
			}
		}
	);

	if (to_refresh) {
    setTimeout("refresh_crawler_status()", 10000);
	}
}
</script>
